<template>
	<view class="login_bg">
		<view class="login_top">
			<view @click="" class="kefu">联系客服</view>
		</view>
		<view class="menuList">
		  <view :class="(currIndex==index) ? 'curr' : ''" v-for="(item,index) in menuList" @click="catchTab(index)">{{item}}</view>
		</view>
		<view v-show="currIndex==0">
			<view class="yzm_login">
				<view class="yzm_login_phone">
					<view class="phone">+86</view>
					<view class="">
						<input type="number" maxlength="11" placeholder="请输入手机号" class="phone_num" v-model="form.mobile">
					</view>
				</view>
			</view>
		</view>
		<view v-show="currIndex==1">
			<view class="yzm_login">
				<view class="yzm_login_phone">
					<view class="phone">+86</view>
					<view class="">
						<input type="number" maxlength="11" placeholder="请输入手机号" class="phone_num" v-model="form.mobile">
					</view>
				</view>
			</view>
			<view class="yzm_pwd">
				<view class="yzm_login_pwd">
					<view class="pwd">密码</view>
					<view class="">
						<input type="password" placeholder="请输入密码" class="pwd_num"  v-model="form.password">
					</view>
				</view>
			</view>
		</view>
		<view>
			<view class="select_radio">
				<checkbox-group @change="allow_protocol" class="protocol">
					<label>
						<checkbox value="1" />同意《用户可见协议》、《可见隐私政策》
					</label>
				</checkbox-group>
			</view>
		</view>
		<view>
			<button :class="(disabled) ? 'grey button' : 'button'" :disabled="disabled" type="primary" @click="getCode" v-show="currIndex===0">获取验证码</button>
			<button :class="(disabled) ? 'grey button' : 'button'" :disabled="disabled"  @click="doLogin" v-show="currIndex==1"  >登录</button>
		</view>
	</view>
</template>

<script>
	import request from '../../utils/http.js'
	export default {
		data() {
		  return {
		    isCheckeddd:false,
		    menuList:['验证码登录','密码登录'],
		    currIndex:0,
		    currIndex1:0,
		    showPopup: false, // 控制弹出层的显示和隐藏  
		   	type: 'bottom',
			form:{
				mobile:'13451532647',
				password:'123456'
			},
			allow:0,
			disabled:true
		  }
		},
		onLoad() {
			
		},
		methods: {
			catchTab(index){
				this.currIndex = index
			},
			getCode(){
				if(this.allow==1){
					request({
						url: '/tools/sendSms',
						method:'post',
						data: {scan:'login',mobile:this.form.mobile},
					}).then(res => {
						let result = res.data;
						if(result.code==1){
							uni.navigateTo({
								url:'/pages/user/yzm?m='+this.form.mobile,
							})
						}else{
							uni.showToast({
								icon:'error',
								title:result.msg
							})
						}
					}).catch(err => {
					  console.error(err);
					});
				}else{
					uni.showToast({
						icon:'error',
						title:"请同意用户协议"
					})
				}
				
			},
			allow_protocol(e){
				if(e.detail.value){
					this.allow = 1;
				}else{
					this.allow = 0;
				}
			},
			doLogin(){
				if(!this.form.mobile){
					uni.showToast({
						icon:'error',
						title:"请填写手机号"
					})
					return false
				}
				if(!this.form.password){
					uni.showToast({
						icon:'error',
						title:"请填写密码"
					})
					return false
				}
				request({
					url: '/Auth/accountLogin',
					method:'post',
					data: this.form,
				}).then(res => {
					let result = res.data;
					if(result.code==1){
						// this.login(result.data)
						uni.setStorageSync("token",result.data.token)
						uni.setStorageSync("nickname",result.data.nickname)
						uni.setStorageSync("sex",result.data.gender)
						uni.redirectTo({
							url:"/pages/index/index",
						})
						
					}else{
						uni.showToast({
							icon:'error',
							title:result.msg
						})
					}
				}).catch(err => {
				  console.error(err);
				});
				// console.log(this.allow)
			}
		},
		watch:{
			'form.mobile':function(){
				if(/^1[3-9]\d{9}$/.test(this.form.mobile)){
					this.disabled = false;
				}else{
					this.disabled = true
				}
			}
		}
  }
</script>

<style>
	@import "../../static/css/login.css";
	@import "../../static/css/iconfont.css";
	.grey{
		background: grey!important;
	}
	.protocol{
		border-radius: 50%;
	}
</style>
